﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Catalogos.aspx.cs" Inherits="entgp.Administracion.Catalogos" %>

<%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Administración de Catalogos</title>
</head>
<body>
    <form id="form1" runat="server">
    <ext:ScriptManager ID="ScriptManager1" runat="server" AjaxMethodNamespace="CatalogoAjax" Theme="Slate">
    </ext:ScriptManager>

    <script type="text/javascript" language="javascript">
        function CargarDatos()
        {
            var r = <%=this.sCodigos.ClientID %>.getById(<%=this.cbxCodigo.ClientID %>.getValue());
            <%=this.txaDescripcion.ClientID %>.setValue(r.data['Descripcion']);
        };
        
        function LimpiarForm()
        {
            <%= this.cbxCodigoCatalogo.ClientID %>.reset();
            <%= this.txtNombre.ClientID %>.reset();
            <%= this.txtDescripcion.ClientID %>.reset();
            <%= this.radSi.ClientID %>.setValue(false);
            <%= this.radNo.ClientID %>.setValue(true);
            <%= this.cbxCodigoCatalogoPadre.ClientID %>.reset();
            <%= this.cbxItemCatalogoPadre.ClientID %>.reset();
            <%= this.hidCodigoCatalodo.ClientID %>.reset();
            <%= nfNumHabUrbano.ClientID %>.setValue(0);
            <%= nfNumHabRural.ClientID %>.setValue(0);
        };
        var EliminarRegistro = function() {
            Ext.MessageBox.show({
               title:"Eliminar Documento",
               msg: "Esta seguro de eliminar el item seleccionado?",
               buttons: Ext.MessageBox.OKCANCEL,
               fn: ConfirmDelDoc,
               icon: Ext.MessageBox.WARNING
           });        
        }
        
        function ConfirmDelDoc(btn)
        {
            if(btn=='ok')
                <%= gdpCatalogos.ClientID %>.deleteSelected();
        };
        
        function habilitarControles(codigo)
        {
            if(codigo=='PROVINCIAFS' || codigo=='PROVINCIAFC')
            {
                <%= nfNumHabUrbano.ClientID %>.show();
                <%= nfNumHabRural.ClientID %>.show();
            }
            else
            {
                <%= nfNumHabUrbano.ClientID %>.hide();
                <%= nfNumHabRural.ClientID %>.hide();
            }
        };
    </script>

    <ext:Store runat="server" ID="sCodigos">
        <Reader>
            <ext:JsonReader ReaderID="Id">
                <Fields>
                    <ext:RecordField Name="Id" />
                    <ext:RecordField Name="Codigo" />
                    <ext:RecordField Name="Descripcion" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store runat="server" ID="sListaCatalogos">
        <Reader>
            <ext:JsonReader ReaderID="Id">
                <Fields>
                    <ext:RecordField Name="Id" />
                    <ext:RecordField Name="Codigo" />
                    <ext:RecordField Name="Descripcion" />
                    <ext:RecordField Name="Nombre" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store runat="server" ID="sItemCatalogoPadre">
        <Reader>
            <ext:JsonReader ReaderID="Id">
                <Fields>
                    <ext:RecordField Name="Id" />
                    <ext:RecordField Name="Codigo" />
                    <ext:RecordField Name="Descripcion" />
                    <ext:RecordField Name="Nombre" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Hidden ID="hidIdCatalogo" runat="server" ></ext:Hidden>
    <ext:Hidden ID="hidCodigoCatalodo" runat="server">
                                            </ext:Hidden>
    <ext:ViewPort ID="ViewPort1" runat="server">
        <Body>
            <ext:BorderLayout ID="BorderLayout1" runat="server">
                <North Collapsible="True" Split="false">
                    <ext:Panel ID="Panel1" BodyStyle="padding:10px;background-color:#E7E4DD;" runat="server"
                        AutoHeight="true" Border="false">
                        <Body>
                            <ext:Panel ID="Panel2" runat="server" BodyStyle="background-color:Transparent;" ButtonAlign="Center"
                                Border="false">
                                <Body>
                                    <ext:FormLayout ID="FormLayout1" runat="server" LabelAlign="Left" LabelWidth="70">
                                        <ext:Anchor Horizontal="50%">
                                            <ext:ComboBox FieldLabel="Codigo" ID="cbxCodigo" StoreID="sCodigos" runat="server"
                                                DisplayField="Codigo" ValueField="Id" EmptyText="Seleccione" Editable="false">
                                            <Listeners>
                                            <Select Handler="CargarDatos();" />
                                            </Listeners>    
                                            </ext:ComboBox>
                                        </ext:Anchor>
                                        <ext:Anchor Horizontal="50%">
                                            <ext:TextArea ID="txaDescripcion" runat="server" FieldLabel="Descripción">
                                            </ext:TextArea>
                                        </ext:Anchor>
                                        <ext:Anchor Horizontal="50%">
                                            <ext:Panel runat="server" ButtonAlign="Center" Border="false">
                                                <Body>
                                                </Body>
                                                <Buttons>
                                                    <ext:Button runat="server" Text="Listar Items">
                                                        <AjaxEvents>
                                                            <Click OnEvent="ListarItems_Click">
                                                            <EventMask ShowMask="true" Msg="Listando Items" />
                                                            </Click>
                                                        </AjaxEvents>
                                                    </ext:Button>
                                                </Buttons>
                                            </ext:Panel>
                                        </ext:Anchor>
                                    </ext:FormLayout>
                                </Body>
                            </ext:Panel>
                        </Body>
                    </ext:Panel>
                </North>
                <Center>
                    <ext:GridPanel ID="gdpCatalogos" runat="server" StoreID="sListaCatalogos" StripeRows="true"
                        AutoWidth="true" Height="250px" Title="Listado de Catalogos" AutoExpandColumn="Descripcion">
                        <TopBar>
                            <ext:Toolbar runat="server">
                                <Items>
                                    <ext:ToolbarButton runat="server" Text="Nuevo Item" Icon="PageAdd">
                                        <Listeners>
                                            <Click Handler="LimpiarForm();#{wItemCatalogo}.show();" />
                                        </Listeners>
                                    </ext:ToolbarButton>
                                </Items>
                            </ext:Toolbar>
                        </TopBar>
                        <ColumnModel ID="ColumnModel1" runat="server">
                            <Columns>
                                <ext:Column Header="Codigo" Width="150" Sortable="true" DataIndex="Codigo" />
                                <ext:Column Header="Nombre" Width="200" Sortable="true" DataIndex="Nombre" />
                                <ext:Column ColumnID="Descripcion" Header="Descripción" Width="250" Sortable="true"
                                    DataIndex="Descripcion" />
                                <ext:CommandColumn Width="120">
                                    <Commands>
                                        <ext:GridCommand CommandName="Editar" Text="Editar" Icon="PageEdit" ToolTip-Text="Editar item">
                                        </ext:GridCommand>
                                        <ext:GridCommand CommandName="Eliminar" Text="Eliminar" Icon="PageDelete" ToolTip-Text="Eliminar item">
                                        </ext:GridCommand>
                                    </Commands>
                                </ext:CommandColumn>
                                
                            </Columns>
                            
                        </ColumnModel>
                        <AjaxEvents>                            
                            <Command OnEvent="Editar_Codigo">
                                <ExtraParams>
                                    <ext:Parameter Name="Comando" Value="command" Mode="Raw" Encode="False" />
                                    <ext:Parameter Name="Id" Value="record.data.Id" Mode="Raw" />
                                </ExtraParams>
                            </Command>
                            
                        </AjaxEvents>
                        <SelectionModel>
                            <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" SingleSelect="true">
                            
                            </ext:RowSelectionModel>
                        </SelectionModel>
                    </ext:GridPanel>
                </Center>
            </ext:BorderLayout>
        </Body>
    </ext:ViewPort>
    <ext:Window ID="wItemCatalogo" runat="server" Icon="Application"
        Title="Información del Item" Modal="True" AutoHeight="true" Width="600px" Y="5"
        CenterOnLoad="true" ShowOnLoad="False" ButtonAlign="Center">
        <Body>
            <ext:FormPanel ID="fpItemCatalogo" runat="server" MonitorPoll="500" MonitorValid="true"
                BodyStyle="padding:5px;" ButtonAlign="Right">
                <Body>
                    <ext:Panel ID="Panel27" runat="server" Border="false" Header="false" BodyStyle="padding:5px;">
                        <Defaults>
                            <ext:Parameter Name="AllowBlank" Value="false" Mode="Raw" />
                            <ext:Parameter Name="MsgTarget" Value="side" />
                        </Defaults>
                        <Body>
                            <ext:FormLayout ID="FormLayout19" runat="server" LabelAlign="Left" LabelWidth="130">
                                <ext:Anchor Horizontal="96%">
                                    <ext:ComboBox ID="cbxCodigoCatalogo" runat="server" FieldLabel="Código Catalogo" AllowBlank="false"
                                        StoreID="sCodigos" DisplayField="Codigo" ValueField="Codigo" Editable="false">
                                        <Listeners>
                                        <Valid Handler="habilitarControles(#{cbxCodigoCatalogo}.getValue());" />
                                        </Listeners>
                                    </ext:ComboBox>
                                </ext:Anchor>
                                <ext:Anchor Horizontal="96%"> 
                                    <ext:TextField FieldLabel="Nombre" ID="txtNombre" runat="server" />
                                </ext:Anchor>
                                <ext:Anchor Horizontal="96%">
                                    <ext:TextArea FieldLabel="Descripción" ID="txtDescripcion" runat="server" />
                                </ext:Anchor>
                                <ext:Anchor Horizontal="96%"> 
                                    <ext:NumberField FieldLabel="# Habitantes Urbano" ID="nfNumHabUrbano" runat="server" AllowBlank="false">
                                    <Listeners>
                                    <Invalid Handler="#{nfNumHabUrbano}.setValue(0);" />
                                    </Listeners>
                                    </ext:NumberField>
                                </ext:Anchor>
                                <ext:Anchor Horizontal="96%">
                                    <ext:NumberField FieldLabel="# Habitantes Rural" ID="nfNumHabRural" runat="server"  AllowBlank="false">
                                    <Listeners>
                                    <Invalid Handler="#{nfNumHabRural}.setValue(0);" />
                                    </Listeners>
                                    </ext:NumberField>
                                </ext:Anchor>
                                <ext:Anchor Horizontal="96%">
                                    <ext:RadioGroup runat="server" FieldLabel="Tiene Padre" ColumnsNumber="2" ItemCls="x-check-group-alt">
                                        <Items> 
                                            <ext:Radio ID="radSi" runat="server" BoxLabel="SI">
                                                <Listeners>
                                                    <Check Handler="if(#{radSi}.getValue()){#{cbxCodigoCatalogoPadre}.enable();#{cbxItemCatalogoPadre}.enable();}" />
                                                </Listeners>
                                            </ext:Radio>
                                            <ext:Radio ID="radNo" runat="server" BoxLabel="NO" Checked="true">
                                                <Listeners>
                                                    <Check Handler="if(#{radNo}.getValue()){#{cbxCodigoCatalogoPadre}.disable();#{cbxItemCatalogoPadre}.disable();}" />
                                                </Listeners>
                                            </ext:Radio>
                                        </Items>
                                    </ext:RadioGroup>
                                </ext:Anchor>
                                <ext:Anchor Horizontal="96%">
                                    <ext:ComboBox ID="cbxCodigoCatalogoPadre" Disabled="true" runat="server" FieldLabel="Código Catalogo"
                                        StoreID="sCodigos" DisplayField="Codigo" ValueField="Codigo"  Editable="false">
                                        <AjaxEvents>
                                            <Select OnEvent="cbxCodigoCatalogoPadre_Select">
                                            </Select>
                                        </AjaxEvents>
                                    </ext:ComboBox>
                                </ext:Anchor>
                                <ext:Anchor Horizontal="96%">
                                    <ext:ComboBox ID="cbxItemCatalogoPadre" Disabled="true" runat="server" FieldLabel="Catalogo Padre"
                                        StoreID="sItemCatalogoPadre" DisplayField="Nombre" ValueField="Id" Editable="false">
                                    </ext:ComboBox>
                                </ext:Anchor>
                            </ext:FormLayout>
                        </Body>
                    </ext:Panel>
                </Body>
            </ext:FormPanel>
        </Body>
        <Buttons>
            <ext:Button runat="server" ID="btnGuardar" Text="Guardar" Icon="Add">
                <Listeners>
                    <Click Handler="if(#{fpItemCatalogo}.getForm().isValid()){return true;}else{#{wItemCatalogo}.show();Ext.Msg.show({icon: Ext.MessageBox.ERROR, msg: 'Todos los campos son obligatorios', buttons:Ext.Msg.OK});return false;}" />
                </Listeners>
                <AjaxEvents>
                <Click OnEvent="btnGuardar_Click" Failure="Ext.Msg.show({icon: Ext.MessageBox.ERROR, msg: 'Ocurrio un error al grabar el item revise los datos e intente nuevamente', buttons:Ext.Msg.OK});" Success="LimpiarForm();#{wItemCatalogo}.hide();Ext.Msg.show({icon: Ext.MessageBox.INFO, msg: 'El item se creo correctamente', buttons:Ext.Msg.OK});">
                <EventMask ShowMask="true" Msg="Guardando Item" />
                </Click>
                
                </AjaxEvents>
            </ext:Button>
            <ext:Button runat="server" ID="btnCancelar" Text="Cancelar" Icon="Stop">
                <Listeners>
                    <Click Handler="LimpiarForm();#{wItemCatalogo}.hide();" />
                </Listeners>
            </ext:Button>
        </Buttons>
    </ext:Window>
    </form>
</body>
</html>
